﻿Imports System.Data.SqlClient

Public Class frmCatalogoAutobusesImportar
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal

    Private Sub frmCatalogoAutobusesImportar_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró al importador de Autobuses")
        Usuario.Ubicacion("Catálogo de Autobuses")
    End Sub
    Private Sub frmCatalogoAutobusesImportar_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System
        ProgressBar1.Value = 0

        dgClasificaciones.DefaultCellStyle.Font = New Font("Segoe UI", 10, FontStyle.Regular)

        Bitacora.RegistraActividad("Ingresó al importador de Autobuses")
        Usuario.Ubicacion("Importador de Autobuses")

        chTodos.Checked = True

        CargaClientes()

    End Sub
    Sub CargaClientes()
        Dim cnConn As New sqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "SELECT * FROM MGW10002 WHERE cTipoCli01=1 AND cCodigoC01 LIKE '%%' Order by cCodigoC01 "

            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql

            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgClasificaciones.Rows.Add(rdBuscar("cidClien01"), True, rdBuscar("cCodigoC01").ToString.Trim, rdBuscar("cRazonSo01").ToString.Trim)
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub ToolStripButton4_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton4.Click
        Dim Buses As New clsCatalogoAutobuses
        Dim i As Integer
        Dim Cont As Integer = 0

        Dim Registros As Integer = 0

        For i = 0 To dgClasificaciones.Rows.Count - 1
            If dgClasificaciones.Rows(i).Cells(1).EditedFormattedValue = True Then
                Registros = Registros + 1
            End If
        Next

        With ProgressBar1
            .Maximum = Registros
            .Minimum = 0
            .Value = 0
            .Visible = True
            Application.DoEvents()

        End With

        For i = 0 To dgClasificaciones.Rows.Count - 1
            If dgClasificaciones.Rows(i).Cells(1).EditedFormattedValue = True Then
                Buses.IdAutobus = dgClasificaciones.Rows(i).Cells(0).Value
                Buses.NoEconomico = dgClasificaciones.Rows(i).Cells(3).Value.ToString.Trim
                Buses.IdRuta = BuscarIntDatoCitra("MGW10002", "cIdValor01", "cIdClien01=" & dgClasificaciones.Rows(i).Cells(0).Value)
                Buses.TipoKms = 0
                Buses.cIdClien01 = dgClasificaciones.Rows(i).Cells(0).Value
                Buses.cCodigoC01 = dgClasificaciones.Rows(i).Cells(2).Value.ToString.Trim
                Buses.cRazonSo01 = dgClasificaciones.Rows(i).Cells(3).Value.ToString.Trim
                Buses.Placas = BuscarStrDatoCitra("MGW10002", "CTEXTOEX01", "cIdClien01=" & dgClasificaciones.Rows(i).Cells(0).Value)
                Buses.Marca = BuscarStrDatoCitra("MGW10002", "CTEXTOEX02", "cIdClien01=" & dgClasificaciones.Rows(i).Cells(0).Value)
                Buses.Modelo = BuscarStrDatoCitra("MGW10002", "CTEXTOEX03", "cIdClien01=" & dgClasificaciones.Rows(i).Cells(0).Value)
                Buses.Serie = BuscarStrDatoCitra("MGW10002", "CTEXTOEX04", "cIdClien01=" & dgClasificaciones.Rows(i).Cells(0).Value)
                Buses.Motor = BuscarStrDatoCitra("MGW10002", "CTEXTOEX05", "cIdClien01=" & dgClasificaciones.Rows(i).Cells(0).Value)
                Buses.Estatus = BuscarStrDatoCitra("MGW10002", "CESTATUS", "cIdClien01=" & dgClasificaciones.Rows(i).Cells(0).Value)
                Buses.Importe01 = 0
                Buses.Importe02 = 0
                Buses.Importe03 = 0
                Buses.Importe04 = 0
                Buses.Importe05 = BuscarIntDatoCitra("MGW10002", "cIdValor02", "cIdClien01=" & dgClasificaciones.Rows(i).Cells(0).Value)
                Buses.Texto01 = ""
                Buses.Texto02 = ""
                Buses.Texto03 = ""
                Buses.Texto04 = ""
                Buses.Texto05 = ""
                Buses.Guardar()
                Cont = Cont + 1
                ProgressBar1.Value = Cont
                Application.DoEvents()
                Bitacora.RegistraActividad("Importo el Autobus " & dgClasificaciones.Rows(i).Cells(3).Value.ToString.Trim)
            End If
        Next

        If Cont > 0 Then
            frmCatalogoAutobuses.CargaAutobuses()
            Close()
        Else
            MsgBox("Debes indicar al menos un Autobus para importar", MsgBoxStyle.Exclamation, "Aviso")
        End If
    End Sub

    Private Sub chTodos_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chTodos.CheckedChanged
        If chTodos.Checked = True Then
            For i = 0 To dgClasificaciones.Rows.Count - 1
                dgClasificaciones.Rows(i).Cells(1).Value = True

            Next
        Else
            For i = 0 To dgClasificaciones.Rows.Count - 1
                dgClasificaciones.Rows(i).Cells(1).Value = False

            Next
        End If
    End Sub
End Class